package com.wsm.system.service.impl;

import com.wsm.common.annotation.RedisCache;
import com.wsm.common.domain.Tree;
import com.wsm.common.enums.BaseEnum;
import com.wsm.common.utils.BuildTree;
import com.wsm.common.utils.SnowFlake;
import com.wsm.system.domain.MenuDO;
import com.wsm.system.mapper.MenuDao;
import com.wsm.system.mapper.RoleMenuDao;
import com.wsm.system.service.MenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional(readOnly = true,rollbackFor = Exception.class)
public class MenuServiceImpl implements MenuService {
	@Autowired
	MenuDao menuMapper;
	@Autowired
	RoleMenuDao roleMenuMapper;

	/**
	 * @param
	 * @return 树形菜单
	 */
	@Cacheable
	@Override
	@RedisCache(value = "menu", flush = false)
	public Tree<MenuDO> getSysMenuTree(Long id) {
		List<Tree<MenuDO>> trees = new ArrayList<Tree<MenuDO>>();
		List<MenuDO> menuDOs = menuMapper.listMenuByUserId(id);
		for (MenuDO sysMenuDO : menuDOs) {
			Tree<MenuDO> tree = new Tree<MenuDO>();
			tree.setId(sysMenuDO.getMenuId().toString());
			tree.setParentId(sysMenuDO.getParentId().toString());
			tree.setText(sysMenuDO.getName());
			Map<String, Object> attributes = new HashMap<>(16);
			attributes.put("url", sysMenuDO.getUrl());
			attributes.put("icon", sysMenuDO.getIcon());
			tree.setAttributes(attributes);
			trees.add(tree);
		}
		// 默认顶级菜单为０，根据数据库实际情况调整
		Tree<MenuDO> t = BuildTree.build(trees);
		return t;
	}

	@Override
	@RedisCache(value = "menu", flush = false)
	public List<MenuDO> list(Map<String, Object> params) {
		List<MenuDO> menus = menuMapper.list(params);
		return menus;
	}

	@Transactional(readOnly = false,rollbackFor = Exception.class)
	@Override
	@RedisCache(value = "menu", flush = true)
	public int remove(Long id) {
		int result = menuMapper.remove(id);
		return result;
	}
	@Transactional(readOnly = false,rollbackFor = Exception.class)
	@Override
	@RedisCache(value = "menu", flush = true)
	public int save(MenuDO menu) {
		List<MenuDO> menuDOList = new ArrayList<>();
		long parentNextId = SnowFlake.nextId();
		menu.setMenuId(parentNextId);
		String perms = menu.getPerms() + ":";
		menu.setPerms("");
		if (menu.getType() == 1) {
			MenuDO sonMenu;
			String name = null;
			String permsVlaue = null;
			for (int i = 0; i <= 10; i++) {
				sonMenu = new MenuDO();
				sonMenu.setMenuId(SnowFlake.nextId());
				sonMenu.setParentId(parentNextId);
				sonMenu.setType(2);
				sonMenu.setOrderNum(i);
				sonMenu.setGmtCreate(new Date());
				sonMenu.setDelFlag(BaseEnum.DEL_FLAF.Not_del.getCode());
				if (i == 0) {//查看权限
					name = "查看";
					permsVlaue = perms + "look";
				} else if (i == 1) {//增加权限
					name = "增加";
					permsVlaue = perms + "add";
				} else if (i == 2) {//修改权限
					name = "修改";
					permsVlaue = perms + "edit";
				} else if (i == 3) {//删除权限
					name = "删除";
					permsVlaue = perms + "remove";
				} else if (i == 4) {//批量删除权限
					name = "批量删除";
					permsVlaue = perms + "batchRemove";
				} else if (i == 5) {//伪删除权限
					name = "伪删除";
					permsVlaue = perms + "removeDel";
				} else if (i == 6) {//批量伪删除权限
					name = "批量伪删除";
					permsVlaue = perms + "batchRemoveDel";
				} else if (i == 7) {//更改状态权限
					name = "更改状态";
					permsVlaue = perms + "change";
				}else if (i == 8) {//更改状态权限
					name = "管理员查看权限";
					permsVlaue = perms + "adminLook";
				}else if (i == 9) {//更改状态权限
					name = "已删除改为未删除";
					permsVlaue = perms + "addOrDel";
				}else if (i == 10) {//更改状态权限
					name = "批量已删除改为未删除";
					permsVlaue = perms + "batchAddOrDel";
				}
				sonMenu.setName(name);
				sonMenu.setPerms(permsVlaue);
				name = null;
				permsVlaue = null;
				menuDOList.add(sonMenu);
			}
		}
		menuDOList.add(menu);
		int r = menuMapper.batchSave(menuDOList);
		return r;
	}

	@Transactional(readOnly = false,rollbackFor = Exception.class)
	@Override
	@RedisCache(value = "menu", flush = true)
	public int update(MenuDO menu) {
		int r = menuMapper.orUpdate(menu);
		return r;
	}

	@Override
	@RedisCache(value = "menu", flush = false)
	public MenuDO get(Long id) {
		MenuDO menuDO = menuMapper.get(id);
		return menuDO;
	}

	@Override
	@RedisCache(value = "menu", flush = false)
	public Tree<MenuDO> getTree() {
		List<Tree<MenuDO>> trees = new ArrayList<Tree<MenuDO>>();
		List<MenuDO> menuDOs = menuMapper.list(new HashMap<>(16));
		for (MenuDO sysMenuDO : menuDOs) {
			Tree<MenuDO> tree = new Tree<MenuDO>();
			tree.setId(sysMenuDO.getMenuId().toString());
			tree.setParentId(sysMenuDO.getParentId().toString());
			tree.setText(sysMenuDO.getName());
			trees.add(tree);
		}
		// 默认顶级菜单为０，根据数据库实际情况调整
		Tree<MenuDO> t = BuildTree.build(trees);
		return t;
	}

	@Override
	@RedisCache(value = "menu", flush = false)
	public Tree<MenuDO> getTree(Long id) {
		// 根据roleId查询权限
		List<MenuDO> menus = menuMapper.list(new HashMap<String, Object>(16));
		List<Long> menuIds = roleMenuMapper.listMenuIdByRoleId(id);
		List<Long> temp = menuIds;
		for (MenuDO menu : menus) {
			if (temp.contains(menu.getParentId())) {
				menuIds.remove(menu.getParentId());
			}
		}
		List<Tree<MenuDO>> trees = new ArrayList<Tree<MenuDO>>();
		List<MenuDO> menuDOs = menuMapper.list(new HashMap<String, Object>(16));
		for (MenuDO sysMenuDO : menuDOs) {
			Tree<MenuDO> tree = new Tree<MenuDO>();
			tree.setId(sysMenuDO.getMenuId().toString());
			tree.setParentId(sysMenuDO.getParentId().toString());
			tree.setText(sysMenuDO.getName());
			Map<String, Object> state = new HashMap<>(16);
			Long menuId = sysMenuDO.getMenuId();
			if (menuIds.contains(menuId)) {
				state.put("selected", true);
			} else {
				state.put("selected", false);
			}
			tree.setState(state);
			trees.add(tree);
		}
		// 默认顶级菜单为０，根据数据库实际情况调整
		Tree<MenuDO> t = BuildTree.build(trees);
		return t;
	}

	@Override
	@RedisCache(value = "menu", flush = false)
	public Set<String> listPerms(Long userId) {
		List<String> perms = menuMapper.listUserPerms(userId);
		Set<String> permsSet = new HashSet<>();
		for (String perm : perms) {
			if (StringUtils.isNotBlank(perm)) {
				permsSet.addAll(Arrays.asList(perm.trim().split(",")));
			}
		}
		return permsSet;
	}

	@Override
	public List<Tree<MenuDO>> listMenuTree(Long id) {
		List<Tree<MenuDO>> trees = new ArrayList<Tree<MenuDO>>();
		List<MenuDO> menuDOs = menuMapper.listMenuByUserId(id);
		for (MenuDO sysMenuDO : menuDOs) {
			Tree<MenuDO> tree = new Tree<MenuDO>();
			tree.setId(sysMenuDO.getMenuId().toString());
			tree.setParentId(sysMenuDO.getParentId().toString());
			tree.setText(sysMenuDO.getName());
			Map<String, Object> attributes = new HashMap<>(16);
			attributes.put("url", sysMenuDO.getUrl());
			attributes.put("icon", sysMenuDO.getIcon());
			tree.setAttributes(attributes);
			trees.add(tree);
		}
		// 默认顶级菜单为０，根据数据库实际情况调整
		List<Tree<MenuDO>> list = BuildTree.buildList(trees, "0");
		return list;
	}

}
